<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.visibility == 'hidden')
          e.style.visibility = 'visible';
       else
          e.style.visibility = 'hidden';
    }
//-->
</script>
<table width="100%">
<tr>
  <td width="50%">
    <table width="100%">
      <tr align="center">
        <td width="20%"><a href="/user"><img src="/images/home_icon_small.png" border="0" style="vertical-align:middle;"/></a><br/>Home</td>
        <td width="20%"><a href="/game_runnings/gamepanel_a?gencode=<%=@gencode%>"><img src="/images/monitor_icon.png" border="0" style="vertical-align:middle;"/></a><br/>Panel A</td>
        <td width="20%"><a href="/game_runnings/gamepanel?gencode=<%=@gencode%>"><img src="/images/monitor_icon.png" border="0" style="vertical-align:middle;"/></a><br/>Panel B</td>
        <td width="20%"><a href="/game_runnings/summary?gencode=<%=@gencode%>"><img src="/images/summary_icon.jpg" border="0" style="vertical-align:middle;"/></a><br/>Summary</td>
        <td><a href="/game_runnings/setting_parameter?gencode=<%=@gencode%>"><img src="/images/inventory_icon.jpg" border="0" style="vertical-align:middle;"/></a><br/>Setting</td>
      </tr>
    </table>
  </td>
  <td width="2%">&nbsp;</td>
  <td>
    <span style="background-color: green; width:350px; font-size: 2em; color:white; vertical-align:middle;">
    Year: <span id="yearpanel"><%=@game.current_year%></span>
    &nbsp;&nbsp;&nbsp;
    Round: <span id="roundpanel"><%=@game.current_round%></span>
    </span>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <%if @game.run_flag=='N'%>
    <img src="/images/stop_icon.jpg" width="50px" style="vertical-align:middle;"/>
    <%else%>
    <img src="/images/start_icon.gif" width="50px" style="vertical-align:middle;"/>
    <%end%>
  </td>
</tr>
</table>
<table width="100%">
  <tr>
    <td>
      <input type="button" value="0.S/H" onclick="toggle_visibility('min_error'); toggle_visibility('max_error'); toggle_visibility('marketpanel'); toggle_visibility('market_remain');"/>
      <input type="button" value="1.Check" onclick="PopupCenter('/game_runnings/b_check/<%=@gencode%>','check_b',950,700);"/>
    </td>
    <%
      min_error = @game.min_error
      max_error = @game.max_error
      min_price = @game.min_price
      max_price = @game.max_price
      game_error = GameError.find(:first, :conditions=>["game_id=? AND year=?", @game.id, @game.current_year])
      if game_error
        min_error = game_error.min_error
        max_error = game_error.max_error
        min_price = game_error.min_price
        max_price = game_error.max_price
      end
    %>
    <td align="center">
      <p style="font-size:40px;">
        <span id="random_market_panel">[ <span id="min_error" style="visibility:hidden;"><%=min_error%></span> , <span id="max_error" style="visibility:hidden;"><%=max_error%></span> ]</span>
        <input type="button" value="2.Start Year" style="vertical-align:middle;" onclick="<%=remote_function(:update=>"yearpanel", :url=>{:controller=>:game_runnings, :action=>:start_year, :gencode=>@gencode}, :with=>"'market_size='+$('marketpanel').innerHTML", :complete=>"refresh_remain()")%>"/>
        <input type="button" value="x.End Year" style="vertical-align:middle;" onclick="<%=remote_function(:update=>"yearpanel", :url=>{:controller=>:game_runnings, :action=>:end_year, :gencode=>@gencode}, :complete=>"refresh_remain()")%>"/>
      </p>
    </td>
    <td align="center">
      <p style="font-size:40px;">
        Price <input type="button" value="4.Start Round" style="vertical-align:middle;" onclick="<%=remote_function(:update=>"roundpanel", :url=>{:controller=>:game_runnings, :action=>:start_round, :gencode=>@gencode}, :with=>"'price='+$('pricepanel').innerHTML", :complete=>"refresh_remain()")%>"/><br/>
        <span id="random_price_panel">[ <%=number_with_precision(min_price,0)%> <input type="button" onclick="<%=remote_function(:url=>{:controller=>"game_runnings", :action=>"random_price", :gencode=>@gencode, :min=>min_price, :max=>max_price},:update=>"pricepanel")%>" value="3.Random" style="vertical-align:middle;"/> <%=number_with_precision(max_price,0)%> ]</span>
      </p>
    </td>
  </tr>
</table>
<table width="100%">
  <tr>
    <td width="10%" valign="middle" align="right"><p style="font-size:40px;">Market Size</p></td>
    <td width="60%" align="right"><p id="marketpanel" style="font-size:110px; color:blue; visibility:hidden;"><%=number_with_delimiter(@game.market_size)%></p></td>
    <td rowspan="2" valign="middle" align="center" bgcolor="black">
      <span id="pricepanel" style="font-size:160px; color:white;"><%=@price%></span>
    </td>
  </tr>
  <tr>
    <td width="10%" valign="middle" align="right"><p style="font-size:40px;">Remains</p></td>
    <td width="60%" align="right">
      <p id="market_remain" style="font-size:110px; color:red; visibility:hidden;">
        <%if @game.market_remain>=0%>
          <%=number_with_delimiter(@game.market_remain)%>
        <%end%>
      </p>
    </td>
  </tr>
</table>
<table width="100%">
  <tr>
    <td width="40%" align="center">
      <img src="/images/search.gif" border="0" onclick="PopupCenter('/game_runnings/zoom_chart_data/<%=@gencode%>','chartPop',950,700);" style="vertical-align:middle;"/>
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="Show Futures Market" onclick="PopupCenter('/game_runnings/show_futures_panel/<%=@gencode%>','fpanel',950,700);"/>
      <span id="chartarea">
        <div id="chartdiv" align="center">Chart will load here</div>
        <script type="text/javascript">
          var chart = new FusionCharts("/charts/Pie2D.swf", "ChartId", "400", "300", "0", "0");
          chart.setXMLUrl('/game_runnings/chart_data/<%=@gencode%>');
          chart.render("chartdiv");
        </script>
      </span>
    </td>
    <td valign="top">
      <span id="resultarea">
        <%if @sales.size>0%>
        <table width="100%" style="border: 3px solid black; font-size: 30px;">
          <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
            <th>Player</th><th>Price</th>
            <th>
              <input type="button" value="Calculate" onclick="PopupCenter('/game_runnings/calculate/<%=@gencode%>','calPop',950,650);"/>
            </th>
            <th>Units Sold</th>
            <%@sales.each do |s|%>
              <%p = s.player%>
              <tr align="center">
                <td align="left"><%=p.name%></td>
                <%
                  price_added = 0
                  game_setting = @game.game_setting
                  if p.level==1
                    price_added = game_setting.level1_price_added
                  end
                  if p.level==2
                    price_added = game_setting.level2_price_added
                  end
                  if p.level==3
                    price_added = game_setting.level3_price_added
                  end
                  if p.level==4
                    price_added = game_setting.level4_price_added
                  end
                %>
                <td>
                  <%=s.price%><%if price_added>0%>(+<%=price_added%>)<%end%>
                </td>
                <td>
                  <input type="button" value="Roll" onclick="<%=remote_function(:url=>{:controller=>"game_runnings", :action=>"allow_roll", :id=>s.id})%>; PopupCenter('/game_runnings/roll/<%=s.id%>','myPop',900,550);"/>
                  <input type="button" value="Clear" onclick="<%=remote_function(:url=>{:controller=>"game_runnings", :action=>"clear_sale", :id=>s.id}, :complete=>"refresh_remain()")%>"/>
                </td>
                <td align="right"><%=number_with_delimiter(s.quantity)%></td>
              </tr>
            <%end%>
          </tr>
        </table>
        <%end%>
      </span>
    </td>
  </tr>
</table>
<script type="text/javascript">
  function refresh_remain(){
    window.location = '/game_runnings/gamepanel?gencode=<%=@gencode%>';
//    new Ajax.Updater('market_remain', '/game_runnings/refresh_remain/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('chartarea', '/game_runnings/refresh_chart/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('roundpanel', '/game_runnings/refresh_round/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('resultarea', '/game_runnings/refresh_result/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('pricepanel', '/game_runnings/refresh_price/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('random_market_panel', '/game_runnings/refresh_random_market/<%=@gencode%>', {asynchronous:true, evalScripts:true});
//    new Ajax.Updater('random_price_panel', '/game_runnings/refresh_random_price/<%=@gencode%>', {asynchronous:true, evalScripts:true});
  }
</script>
<%if @game.game_setting.show_market=='N'%>
  <script type="text/javascript">
    toggle_visibility('min_error'); toggle_visibility('max_error'); toggle_visibility('marketpanel'); toggle_visibility('market_remain');
  </script>
<%end%>